package cn.glutnn.controller;


import cn.glutnn.entity.*;
import cn.glutnn.entity.dto.OrderConditionDTO;
import cn.glutnn.service.OrderService;
import cn.glutnn.service.PurchaseDetailService;
import cn.glutnn.service.PurchaseService;
import cn.glutnn.service.SellPurchaseService;
import cn.glutnn.utils.ThreadLocalUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * (Order)表控制层
 *
 * @author msh
 * @since 2024-10-25 09:35:09
 */
@RestController
@RequestMapping("/order")
@Tag(name = "订单管理模块")
public class OrderController {
    /**
     * 服务对象
     */

    @Autowired
    private OrderService orderService;

    @Autowired
    private PurchaseService purchaseService;

    @Autowired
    private PurchaseDetailService purchaseDetailService;


    @Autowired
    private SellPurchaseService sellPurchaseService;


    // 获取所有订单，分页查询
    @Operation(summary = "获取所有订单，分页查询")
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(orderService.page(new Page<>(pageNum, pageSize)));
    }

    // 获取所有销售订单，分页查询
    @Operation(summary = "获取所有销售订单，分页查询")
    @GetMapping("/findByGoods")
    public Result findByGoodsList(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Order> page = new Page<>(pageNum, pageSize);
        return Result.success(orderService.lambdaQuery().eq(Order::getType, "goods").page(page));
    }

    // 获取所有需求订单，分页查询
    @Operation(summary = "获取所有需求订单，分页查询")
    @GetMapping("/findByNeeds")
    public Result findByNeeds(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Order> page = new Page<>(pageNum, pageSize);
        return Result.success(orderService.lambdaQuery().eq(Order::getType, "needs").page(page));
    }

    // 根据条件查询接口
    @Operation(summary = "根据条件查询接口")
    @GetMapping("/searchAlIByCondition")
    public Result searchAlIByCondition(@RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "10") Integer pageSize,
                                       OrderConditionDTO orderConditionDTO) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        if (orderConditionDTO.getType() != null) {
            wrapper.eq(Order::getType, orderConditionDTO.getType());
        }
        if (orderConditionDTO.getContent() != null) {
            wrapper.like(Order::getContent, orderConditionDTO.getContent());
        }
        if (orderConditionDTO.getOwnName() != null) {
            wrapper.like(Order::getOwnName, orderConditionDTO.getOwnName());
        }
        return Result.success(orderService.page(new Page<>(pageNum, pageSize), wrapper));
    }

    // 添加订单
    @Operation(summary = "添加订单")
    @PostMapping
    public Result add(@Valid @RequestBody @Param("Order") Order order) {
        try {
            Map map = ThreadLocalUtil.get();
            order.setUpdateTime(new Date());
            order.setOwnName((String) map.get("username"));
            orderService.save(order);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        }
        return Result.success();
    }

    // 根据id修改订单
    @Operation(summary = "根据id修改订单")
    @PutMapping("/{id}")
    public Result updateById(@PathVariable("id") Integer id, @RequestBody Order order) {
        order.setOrderId(id);
        try {
            orderService.lambdaUpdate().eq(Order::getOrderId, id).update(order);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("修改失败");
        }
        return Result.success();
    }

    @Operation(summary = "根据id查询订单")
    @GetMapping("/{id}")
    public Result findOrderById(@PathVariable("id") Integer id) {
        if (id == null) {
            return Result.error("id为空");
        }
        return Result.success(orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderId, id)));
    }

    // 根据id删除订单
    @Operation(summary = "根据id删除订单")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        try {
            LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Order::getOrderId, id);
            orderService.remove(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
        return Result.success();
    }


    //根据登录用户
    @Operation(summary = "根据登录用户查询我购买的订单selectBuys")
    @GetMapping("/selectBuys")
    public Result<List<Purchase>> selectBuys() {
        return purchaseService.selectByPurchaseType();
    }


    @GetMapping("/selectBuysDetail/{id}")
    public Result<PurchaseDetail> selectBuysDetail(@PathVariable("id") Integer purchaseId) {
        LambdaQueryWrapper<PurchaseDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PurchaseDetail::getPurchaseId, purchaseId);

        return Result.success(purchaseDetailService.getOne(wrapper));
    }

    //根据登录用户查询我卖出的订单selectSells
    @Operation(summary = "根据登录用户查询我卖出的订单selectSells")
    @GetMapping("/selectSells")
    public Result<List<SellPurchase>> selectSells() {
        return sellPurchaseService.selectByName();
    }


}

